package com.nmc.web.controller;

import com.nmc.web.entity.Ticket;
import com.nmc.web.service.TicketService;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.*;

@Controller
public class TicketController {
    @Resource
    private TicketService ticketService;
    private SimpleDateFormat dateTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    // 给安卓展览名与用户名
    @RequestMapping("/exhibitionAndUser")
    @ResponseBody
    public Map<String, Object> getExhibitionAndUser() {
        Map<String, Object> result = new LinkedHashMap<>();

        List<Ticket> getExhibition = ticketService.selectExhibition();
        List<Ticket> getUser = ticketService.selectUser();

        List<Map<String, Object>> exhibitionList = new ArrayList<>();
        List<Map<String, Object>> userList = new ArrayList<>();

        for (Ticket exhibition1 : getExhibition) {
            Map<String, Object> exhibition = new LinkedHashMap<>();
            exhibition.put("No", exhibition1.getEid());
            exhibition.put("exhibition", exhibition1.getExhibition());
            exhibitionList.add(exhibition);
        }
        for (Ticket user1 : getUser) {
            Map<String, Object> user = new LinkedHashMap<>();
            user.put("No", user1.getUid());
            user.put("user", user1.getUser_name());
            userList.add(user);
        }
        result.put("exhibitionList", exhibitionList);
        result.put("userList", userList);
        return result;

    }

    // 识别条形码 改状态
    @RequestMapping("/clearingTicket")
    @ResponseBody
    public Map<String, Object> clearingTicket(Ticket saveTicket, int exhibition_id, int clearing_name_id, String ticket_id) {
        Map<String, Object> result = new LinkedHashMap<>();
        try {
            //查询该票号是否存在
            List<Ticket> tryIsExist = ticketService.selectIsClearing(ticket_id);
            if (tryIsExist.size() != 0) {
                for (Ticket ticket : tryIsExist) {
                    //该票号状态是否为已结算 并且 展览id相等
                    if (ticket.getClearing_status() == 0 && ticket.getExhibition_id() == exhibition_id) {
                        saveTicket.setClearing_name_id(clearing_name_id);
                        saveTicket.setExhibition_id(exhibition_id);
                        saveTicket.setTicket_id(ticket_id);
                        saveTicket.setClearing_date(dateTime.format(new Date()));
                        ticketService.updateStatus(saveTicket);
                        result.put("code", 200);
                        result.put("message", "票号" + ticket_id + "结算成功");
                    } else if (ticket.getClearing_status() == 1 && ticket.getExhibition_id() == exhibition_id) {
                        result.put("code", 301);
                        result.put("message", "票号" + ticket_id + "已结算,结算人:" + ticket.getUser_name() + "!");
                    }
                }
            } else if (tryIsExist.size() == 0) {
                result.put("code", 300);
                result.put("message", "票号" + ticket_id + "不存在!");
            }
        } catch (Exception e) {
            result.put("code", 400);
            result.put("message", "保存失败,未知错误:" + e);
            System.out.println(e.getMessage());
        }

        return result;
    }

    // 结算查询 可以按日期/展览查询
    @RequestMapping("/searchClearing")
    @ResponseBody
    public Map<String, Object> searchClearing(int exhibition_id, String clearing_date) {
        Map<String, Object> result = new LinkedHashMap<>();
        List<Ticket> allClearing = ticketService.selectClearing(exhibition_id);
        List<Ticket> getExhibition = ticketService.selectExhibition();

        List<Map<String, Object>> info = new ArrayList<>();
        int i = 0;
        try {
            if (allClearing.size() != 0) {
                for (Ticket ticket : allClearing) {
                    if (clearing_date.equals(ticket.getClearing_date().substring(0, 10)) && exhibition_id == ticket.getExhibition_id()) {
                        i = i + 1;
                    } else if (clearing_date.equals("all") && exhibition_id == ticket.getExhibition_id()) {
                        Map<String, Object> infoTemp = new LinkedHashMap<>();
                        infoTemp.put("clearing_date", ticket.getClearing_date().substring(0, 19));
                        infoTemp.put("exhibition", ticket.getExhibition());
                        infoTemp.put("ticketNum", 50);
                        info.add(infoTemp);
                    }
                }
                if (!clearing_date.equals("all")) {
                    Map<String, Object> infoTemp = new LinkedHashMap<>();
                    infoTemp.put("clearing_date", clearing_date);
                    for (Ticket tic : getExhibition) {
                        if (tic.getEid() == exhibition_id)
                            infoTemp.put("exhibition", tic.getExhibition());
                    }
                    infoTemp.put("ticketNum", i * 50);
                    info.add(infoTemp);
                }

                result.put("code", 200);
                result.put("search_result", info);
            } else {
                result.put("code", 300);
                Map<String, Object> infoTemp = new LinkedHashMap<>();
                infoTemp.put("clearing_date", "error");
                infoTemp.put("exhibition", "无结算信息");
                infoTemp.put("ticketNum", 0);
                info.add(infoTemp);
                result.put("search_result", info);
            }


        } catch (Exception e) {
            result.put("code", 400);
            Map<String, Object> infoTemp = new LinkedHashMap<>();
            infoTemp.put("clearing_date", "error");
            infoTemp.put("exhibition", e);
            infoTemp.put("ticketNum", 0);
            info.add(infoTemp);
            result.put("search_result", infoTemp);
            System.out.println(e.getMessage());
        }
        return result;
    }
}
